\chapter{Database Implementation}
We will start with describing the ontology model used for creating hierarchical structure of our domain. Then we will explain how to connect and work with the Virtuoso database. Then we will clarify how the extracted information will be converted into the graph and stored in the database.

\section{Ontology}
This section is a recapitulation of the theoretical section Modeling the information\ref{sec:Ontology}. Informations are divided into classes:
\begin{itemize}
	\item \emph{Terms} - Contains information about subjects and objects, its URIs, lemmas and textual representations
	\item \emph{Predicates} - Contains information about predicates, its URIs, lemmas and textual representations
	\item \emph{Terminologies} - Contains information about terminologies, its URIs, lemmas, textual representations, overall frequencies, frequencies, number of documents in that occur, TF-IDF
	\item \emph{Document} - Contains document name, path to the original document, sentences, terms, predicates, terminologies count, triples and terminologies
\end{itemize}

The ontology is shown in the Appendix B\ref{app:Ontology}. When we were designing the ontology, we were focusing on how to hierarchically structure informations. What is relevant for the document and what is common for all documents. Resource (term, predicate, terminology) instance is relevant only for a document and it denotes that the document contains that kind of information. The textual representation is not relevant for it because it denotes only instance of a particular resource object. Therefore it does not need to be in the document's graph and we can group the textual representations in one common graph. We have created graphs that has well known URI's, textual representations and lemma of the resources. Using URI's prefix \emph{semjobKB:}\url{http://datlowe.org/semjobKB/data/document/} the graphs are:

\begin{itemize}
	\item{semjobKB:term\#} for subjects and objects, graph contains individual textual representations of the resources and their lemmas
	\item{semjobKB:predicate\#} for predicates, graph contains individual textual representations of the resources and its lemmas
	\item{semjobKB:Terminology\#} for terminologies, graph contains individual textual representations of the resources, its lemmas, overall frequencies, number of documents in that occur
\end{itemize}

To optimize searching based on the textual representation of some resources, we do not need to go trough all documents, all we need is to look into these graphs, get the resource URIs having the same textual representations and search for documents based these URIs. This ontology allows us to efficiently store the informations and provide fast searching services.

\section{Working with Virtuoso database}
To work with the Virtuoso database from the application we have decided to look for a \emph{JAVA API} that will provide us with methods to connect, update, store and retrieve data. That functionality is available in the open source Semantic framework called \href{http://virtuoso.openlinksw.com/dataspace/doc/dav/wiki/Main/VirtJenaProvider}{Jena}. \emph{Jena's} subproject called \emph{virt-jena} \url{https://github.com/srdc/virt-jena} is especially designed to work with Virtuoso database and therefore we are using this library in the application. All it needs is to set the database url location, user login and password that are injected into the application in \emph{config.properties} file. The library enables to create Virtuoso graphs, fill them with triples and store them. The \emph{virt-jena} does not have any tutorials only \emph{Javadoc} documentation and few example test that are part of the project.

\section{Converting and storing extracted informations into a graph}
The extracted informations that has been described in the previous chapter needs to be converted into the graph based on the ontology. We have list of triples, and list of terminologies with their frequencies. The process of converting informations into the graph:
\begin{enumerate}
	\item Check if the document is exists in the database, if yes then:		
	\begin{enumerate}			
		\item For each terminology URIs in the document subtracts the overall frequency by the frequency of the terminology and decrease by one the number of documents in which the terminology occurs
		\item Remove the graph from the database and create an empty one
	\end{enumerate}
	\item else:
	\begin{enumerate}			
		\item Create an empty graph
	\end{enumerate}
	\item For each terminology:
	\begin{enumerate}
		\item Escape all characters in the lemma that are not allowed in URI and create URI from the term prefix {semjobKB:Terminology\#} and this escaped lemma.
		\item Create new triple for terminology, predicate semjobKB:hasStringRepresentation and literal having the textual representation of the terminology
		\item Create new triple for terminology, predicate semjobKB:hasLemma and literal having the lemma		
		\item Crate or update the terminology overall frequency and number of occurrences
		\item Add the triples into the terminology graph				
		\item Create triple from terminology resource URI, has:Frequency predicate and literal filled with the frequency
		\item Add it into the document graph
	\end{enumerate}	
	\item For each triple:
	\begin{enumerate}
		\item Extract the subject and object from the triple
		\item Escape all characters in the lemma that are not allowed in URI and create URI from the term prefix {semjobKB:term\#} and this escaped lemma.
		\item Create new triple for subject and object, predicate semjobKB:hasStringRepresentation and literal having the textual representation of the subject or object
		\item Create new triple for subject and object, predicate semjobKB:hasLemma and literal having the lemma
		\item Add the triples into the term graph
		\item Make the same for predicate and add the textual representation triples into the predicate graph
		\item Create triple from subject, predicate, object resource URIs and add it into the document graph
	\end{enumerate}
	\item Create triples containing the sentences, terminology, terms, predicates count, language, document name, document path and store them into the graph.
	\item Method graph.close() commits the changes made and uploads the graph into the database
\end{enumerate}

This algorithm ensures that when we will process the same document again, maybe with different search strategies we wont loose terminology overall frequencies and the number of occurrences. The predicates used to create triples about document details and statistics are in the ontology and can be found in the Appendix.

\section{Implementation}
Implementation is done in module \emph{dbVirtuoso} in a class {DatabaseService} that implements \emph{IDatabaseService} and provides method \emph{(IDocumentDetail storeDocument(IExtractedKnowledge extractedKnowledge, File documentFile, ELanguage language))}. The parameters are: extracted knowledge, file containing the original document and the language of the document. The language has default value set for the Czech language, but we were counting with other languages that might appear in the documents later on. The method \emph{storeDocument} after successful creation and saving the graph returns the graph details to be shown to a user in the application GUI. We do not provide the functionality to remove the document from the database.